﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
namespace NG
{
    class SubAreaKsfHandler:ISubAreaKsf
    {
        private string tableName = "[dbo].[SubAreaKsf]";
        public void addSubAreaKsf(SubAnalaysisArea sAA, KSF ksf, AnalysisArea AA)
        {
            DBConnect con = new DBConnect();
            string tbn = "[" + con.dbName + "]." + this.tableName;
            if (con.OpenConnection() == false)
            {
                throw new System.Exception("Couldn't connect to the Database");
            }
            string query = string.Format("INSERT INTO" + tbn + "(SubAreaCode,KSFcode,AreaCode)VALUES("
                + "{0},'{1}','{2}');",sAA.subAreaCode,ksf.ksfCode,AA.areaCode);


            SqlCommand cmd = new SqlCommand(query, con.connection);
            cmd.ExecuteNonQuery();
            if (con.CloseConnection() == false)
            {
                throw new System.Exception("Couldn't disconnect from the Database");
            }
        }

        public void deleteSubAreaKsf(SubAnalaysisArea sAA, KSF ksf, AnalysisArea AA)
        {
            DBConnect con = new DBConnect();
            string tbn = "[" + con.dbName + "]." + this.tableName;

            if (con.OpenConnection() == false)
            {
                throw new System.Exception("Couldn't connect to the Database");
            }

            string query = string.Format("DELETE FROM " + tbn + " WHERE SubAreaCode = {0} AND KSFcode = {1} AND AreaCode = {2}  ",sAA.subAreaCode,
                ksf.ksfCode,AA.areaCode);
            SqlCommand cmd = new SqlCommand(query, con.connection);
            cmd.ExecuteNonQuery();

            if (con.CloseConnection() == false)
            {
                throw new System.Exception("Couldn't disconnect from the Database");
            }
        }
    }
}
